﻿using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.Fonts;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats;

namespace Ls.ImageSharep.App
{
    public partial class UCHome : UserControl
    {
        public UCHome()
        {
            InitializeComponent();
        }

        private void btnchose_Click_1(object sender, EventArgs e)
        {
            if (!frmImagMain.Instance.PnlContainer.Controls.ContainsKey("UCMerge"))
            {
                UCMerge un = new UCMerge();
                un.Dock = DockStyle.Fill;
                frmImagMain.Instance.PnlContainer.Controls.Add(un);
            }
            frmImagMain.Instance.PnlContainer.Controls["UCMerge"].BringToFront();
            frmImagMain.Instance.BackButton.Visible = true;
        }

        private void UCHome_Load(object sender, EventArgs e)
        {

            pbhome.Image = System.Drawing.Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/images/home.jpg");
            this.pbhome.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        private void btnRoate_Click(object sender, EventArgs e)
        {
            using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(LsApp.ImgToByt(pbhome.Image)))
            {
                //using (FileStream output = File.OpenWrite(AppDomain.CurrentDomain.BaseDirectory +"rotate.jpeg"))
                //{
                //    image.Mutate(x => x.Rotate(RotateMode.Rotate180));
                //    image.Save(output, new JpegEncoder());

                //}

                //显示在picturebox 中 
                image.Mutate(x => x.Rotate(RotateMode.Rotate180));

                //MemoryStream memoryStream = new MemoryStream();
                //image.Save(memoryStream, new JpegEncoder());

                pbhome.Image = System.Drawing.Image.FromStream(LsApp.ImgToMemoryStream(image));
                pbhome.SizeMode = PictureBoxSizeMode.StretchImage;
            }


            //using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(getImageStream(pbhome.Image)))
            //using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(AppDomain.CurrentDomain.BaseDirectory + "/images/home.jpg"))
            //{
            //    using (FileStream output = File.OpenWrite(AppDomain.CurrentDomain.BaseDirectory + "homes.jpeg"))
            //    {

            //        image.Save(output, new JpegEncoder());

            //    }
            //}
        }
        private void btnzone_Click(object sender, EventArgs e)
        {
            using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(LsApp.ImgToByt(pbhome.Image)))
            {
              
                //显示在picturebox 中 
                image.Mutate(x => x.Resize(200, 200));

                pbhome.Image = System.Drawing.Image.FromStream(LsApp.ImgToMemoryStream(image));
                pbhome.SizeMode = PictureBoxSizeMode.AutoSize;
            }
        }
        private void btncorp_Click(object sender, EventArgs e)
        {
            using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(LsApp.ImgToByt(pbhome.Image)))
            {

                //显示在picturebox 中 
                image.Mutate(x => x.Crop(image.Width / 2, image.Height / 2));

                pbhome.Image = System.Drawing.Image.FromStream(LsApp.ImgToMemoryStream(image));
                pbhome.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }

        private void btnsy_Click(object sender, EventArgs e)
        {
            using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(LsApp.ImgToByt(pbhome.Image)))
            {
                string drawText = "土豆你好 Hello World!";
                //获取该文件绘制所需的大小
                TextOptions textOptions = new(LsApp.GetFont(20));
                var size = TextMeasurer.Measure(drawText, textOptions);
                //绘制.这里是右下角, 也可以加入参数动态处理左上/右下/居中等...
                //绘制图片等也是类似
                image.Mutate(x =>
                x.DrawText(drawText, LsApp.GetFont(20), SixLabors.ImageSharp.Color.WhiteSmoke,
                    new SixLabors.ImageSharp.PointF(image.Width - size.Width - 3, image.Height - size.Height - 3))
                );

                pbhome.Image = System.Drawing.Image.FromStream(LsApp.ImgToMemoryStream(image));
                pbhome.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }
        private void btncary_Click(object sender, EventArgs e)
        {
            using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(LsApp.ImgToByt(pbhome.Image)))
            {

                //显示在picturebox 中 
                image.Mutate(x => x.Grayscale());//转灰度图

                pbhome.Image = System.Drawing.Image.FromStream(LsApp.ImgToMemoryStream(image));
                pbhome.SizeMode = PictureBoxSizeMode.StretchImage;
            }
           
        }
        private void btnreset_Click(object sender, EventArgs e)
        {
            pbhome.Image = System.Drawing.Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/images/home.jpg");
            this.pbhome.SizeMode = PictureBoxSizeMode.StretchImage;
        }
        #region 私有方法
        //private  Stream getImageStream(System.Drawing.Image image)
        //{
        //    Bitmap bb = new Bitmap(image);
        //    System.IO.MemoryStream stream1 = new System.IO.MemoryStream();
        //    bb.Save(stream1, System.Drawing.Imaging.ImageFormat.Bmp);
        //    Byte[] nn = stream1.ToArray();
        //    return stream1;
        //}

      
        #endregion

       
    }
}
